﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Team4Mock.Entity;
namespace Team4Mock.UserControl.Organisation
{
    public partial class CreateOrg : System.Web.UI.UserControl
    {
        OrganisationEntity oe=new OrganisationEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dlNational.DataSource = oe.GetCountry();
                dlNational.DataBind();
                
                rpOrgSpec.DataSource = oe.GetOrgSpec();
                rpOrgSpec.DataBind();
                
                rpAcc.DataSource = oe.GetAccred();
                rpAcc.DataBind();

                rpBene.DataSource = oe.GetSerBenifitCapa();
                rpBene.DataBind();

                rpSerBarr.DataSource = oe.GetSerBarrieCapa();
                rpSerBarr.DataBind();

                rpSerDisCap.DataSource = oe.GetSerDisaCapa();
                rpSerDisCap.DataBind();

                rpSerEth.DataSource = oe.GetEthicCapa();
                rpSerEth.DataBind();

                rpSerPerCap.DataSource = oe.GetSerPersoCapa();
                rpSerPerCap.DataBind();
            }
        }
        protected void btnBack1_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Organisation.aspx");
        }

        protected void btnCreate1_Click(object sender, EventArgs e)
        {
            if (txtOrgName.Text == "" || txtShortDes.Text == "" || txtAdd1.Text == "" || txtPhone.Text == "" ||
                    hdidBus.Value == "" || hdsic.Value == "" || PostCode.Value == "")
            {
                lb.Text = "Input Mandatory fields";
            }
            else
            {
                if (InsertOrg() == true)
                {
                    InsertAcc();
                    InsertBarrier();
                    InsertBenefit();
                    InsertDisa();
                    InsertEthnic();
                    InsertPeso();
                    InsertSpec();
                    lb.Text = "Insert Success";
                }
                else
                {
                    lb.Text = "Insert Failure";
                }
            }

        }
        //Insert Org
        bool InsertOrg()
        {
            try
            {
                int contactid = int.Parse(idContact.Value);
                string idbus = hdidBus.Value;
                string postcode = PostCode.Value;
                string sic = hdsic.Value;
                string town = Town.Value;
                string county = County.Value;
                string national = dlNational.SelectedValue;

                oe.OrganizationName = txtOrgName.Text.Trim();
                oe.OrganizationShortDescription = txtShortDes.Text;
                oe.LeadContact = contactid;
                if (ckPreferred.Checked == true)
                    oe.PreferredOrganization = 1;
                else
                    oe.PreferredOrganization = 0;
                if (ckExpress.Checked == true)
                    oe.ExpressionOfOrganization = 1;
                else
                    oe.ExpressionOfOrganization = 0;
                oe.TypeOfBusiness = idbus;
                oe.SICCode = sic;
                oe.OrFullDescription = txtFullDess.Text;
                oe.AddressLine1 = txtAdd1.Text;
                oe.AddressLine2 = txtAdd2.Text;
                oe.AddressLine3 = txtAdd3.Text;
                oe.PostCode = postcode;
                oe.Town = town;
                oe.County = county;
                oe.Country = national;
                oe.PhoneNumber = txtPhone.Text;
                oe.Fax = txtFax.Text;
                oe.WebAddress = txtWeb.Text;
                oe.ChartyNumber = txtCharity.Text;
                oe.CompanyNumber = txtCompany.Text;
                oe.IsActive = 1;
                oe.Insert(oe);
                return true;
            }
            catch
            {
                return false;
            }
        }

        //Acc
        void InsertAcc()
        {
            int Orgid = oe.GetID();
            oe.Organizationid = Orgid;
            for (int i = 0; i < rpAcc.Items.Count; i++)
            {
                CheckBox ck = (CheckBox)rpAcc.Items[i].FindControl("ckAcc");
                if (ck != null)
                {
                    if (ck.Checked == true)
                    {
                        TextBox txt = (TextBox)rpAcc.Items[i].FindControl("txtidAcc");
                        oe.Refid = int.Parse(txt.Text);
                        oe.InsertAcc(oe);
                    }
                }
            }
        }
        //Barrier
        void InsertBarrier()
        {
            int Orgid = oe.GetID();
            oe.Organizationid = Orgid;
            for (int i = 0; i < rpSerBarr.Items.Count; i++)
            {
                CheckBox ck = (CheckBox)rpSerBarr.Items[i].FindControl("ckBarr");
                if (ck != null)
                {
                    if (ck.Checked == true)
                    {
                        TextBox txt = (TextBox)rpSerBarr.Items[i].FindControl("txtidBarr");
                        oe.Refid = int.Parse(txt.Text);
                        oe.InsertBarrier(oe);
                    }
                }
            }
        }

        //Benefits
        void InsertBenefit()
        {
            int Orgid = oe.GetID();
            oe.Organizationid = Orgid;
            for (int i = 0; i < rpBene.Items.Count; i++)
            {
                CheckBox ck = (CheckBox)rpBene.Items[i].FindControl("ckBene");
                if (ck != null)
                {
                    if (ck.Checked == true)
                    {
                        TextBox txt = (TextBox)rpBene.Items[i].FindControl("txtidBene");
                        oe.Refid = int.Parse(txt.Text);
                        oe.InsertBenefit(oe);
                    }
                }
            }
        }
        //Disa
        void InsertDisa()
        {
            int Orgid = oe.GetID();
            oe.Organizationid = Orgid;
            for (int i = 0; i < rpSerDisCap.Items.Count; i++)
            {
                CheckBox ck = (CheckBox)rpSerDisCap.Items[i].FindControl("ckDis");
                if (ck != null)
                {
                    if (ck.Checked == true)
                    {
                        TextBox txt = (TextBox)rpSerDisCap.Items[i].FindControl("txtidDis");
                        oe.Refid = int.Parse(txt.Text);
                        oe.InsertDisa(oe);
                    }
                }
            }
        }
        //Ethnic
        void InsertEthnic()
        {
            int Orgid = oe.GetID();
            oe.Organizationid = Orgid;
            for (int i = 0; i < rpSerEth.Items.Count; i++)
            {
                CheckBox ck = (CheckBox)rpSerEth.Items[i].FindControl("ckEth");
                if (ck != null)
                {
                    if (ck.Checked == true)
                    {
                        TextBox txt = (TextBox)rpSerEth.Items[i].FindControl("txtidEth");
                        oe.Refid = int.Parse(txt.Text);
                        oe.InsertEthnic(oe);
                    }
                }
            }
        }
        //Perso
        void InsertPeso()
        {
            int Orgid = oe.GetID();
            oe.Organizationid = Orgid;
            for (int i = 0; i < rpSerPerCap.Items.Count; i++)
            {
                CheckBox ck = (CheckBox)rpSerPerCap.Items[i].FindControl("ckPer");
                if (ck != null)
                {
                    if (ck.Checked == true)
                    {
                        TextBox txt = (TextBox)rpSerPerCap.Items[i].FindControl("txtidPer");
                        oe.Refid = int.Parse(txt.Text);
                        oe.InsertPerso(oe);
                    }
                }
            }
        }
        //Spec
        void InsertSpec()
        {
            int Orgid = oe.GetID();
            oe.Organizationid = Orgid;
            for (int i = 0; i < rpOrgSpec.Items.Count; i++)
            {
                CheckBox ck = (CheckBox)rpOrgSpec.Items[i].FindControl("ckSpec");
                if (ck != null)
                {
                    if (ck.Checked == true)
                    {
                        TextBox txt = (TextBox)rpOrgSpec.Items[i].FindControl("txtidSpec");
                        oe.Refid = int.Parse(txt.Text);
                        oe.InsertSpec(oe);
                    }
                }
            }
        }
    }
}